package v1

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"

	bc "application/blockchain"
	"application/pkg/app"
)

type HouseTestBody struct {
	Owner   string `json:"owner" form:"owner" binding:"required"`
	IdCard  string `json:"id_card" form:"id_card" binding:"required"`
	HouseId string `json:"house_id" form:"house_id" binding:"required"`
	Status  string `json:"status" form:"status" `
}

func CreateHouseTest(c *gin.Context) {
	appG := app.Gin{C: c}
	body := new(HouseTestBody)
	//解析Body参数
	if err := c.ShouldBind(body); err != nil {
		appG.Response(http.StatusBadRequest, "失败", fmt.Sprintf("参数出错%s", err.Error()))
		return
	}
	if body.Status == "" {
		body.Status = "true"
	}
	var bodyBytes [][]byte
	bodyBytes = append(bodyBytes, []byte(body.Owner))
	bodyBytes = append(bodyBytes, []byte(body.IdCard))
	bodyBytes = append(bodyBytes, []byte(body.HouseId))
	bodyBytes = append(bodyBytes, []byte(body.Status))
	//调用智能合约
	resp, err := bc.ChannelExecute("createHouseTest", bodyBytes)
	if err != nil {
		appG.Response(http.StatusInternalServerError, "失败", err.Error())
		return
	}
	var data map[string]interface{}
	if err = json.Unmarshal(bytes.NewBuffer(resp.Payload).Bytes(), &data); err != nil {
		appG.Response(http.StatusInternalServerError, "失败", err.Error())
		return
	}
	appG.Response(http.StatusOK, "成功", data)
}
